#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 5,M = 1e6;
ll t,n,a[N],opt[2][M],ans;
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1;i <= n;i++)
        {
            cin >> a[i];
            for (int b = 0;b <= 1;b++)
            {
                if (a[i] == a[i - 1])
                    for (int j = 1;j < i;j++) opt[b][a[j]] += a[i];
                for (int j = 1;j < i;j++)
                    opt[b][a[i - 1]] = max(opt[b][a[i - 1]],opt[!b][a[j]] + (a[i] == a[j] ? a[i] : 0));
            }
        }
        for (int j = 1;j <= n;j++) ans = max(ans,opt[0][a[j]]);
        for (int j = 1;j <= n;j++) ans = max(ans,opt[1][a[j]]);
        cout << ans << endl;
        ans = 0;
        memset(opt,0,sizeof(opt));
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
